//
//  RPictureInfoViewController.m
//  Riji
//
//  Created by liujianlong on 2022/8/22.
//

#import "RPictureInfoViewController.h"
#import "RPictureInfoCell.h"

@interface RPictureInfoViewController ()<UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *tool;
@property (weak, nonatomic) IBOutlet UILabel *date;
@property (weak, nonatomic) IBOutlet UILabel *content;

@end

@implementation RPictureInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.collectionView.hidden = NO;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.index - 1 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setupNaviBarColor:[UIColor colorWithHexString:@"#2F88EE"] titleColor:[UIColor whiteColor]];
}
- (void)initUI{
    self.view.backgroundColor = [UIColor blackColor];
    self.title = [NSString stringWithFormat:@"%d/%d",self.index,(int)self.model.data.count];
    self.date.text = [NSDate transToDate:LS(@"yyyy年MM月dd日") :self.model.stamp];
    self.content.text = self.model.content;
    self.collectionView.hidden = YES;
    [self.collectionView registerNib:[RPictureInfoCell nib] forCellWithReuseIdentifier:[RPictureInfoCell ID]];
    [self setupNaviBarColor:[UIColor blackColor] titleColor:[UIColor whiteColor]];
}
-(void)backBtnAction{
    [self.navigationController popViewControllerAnimated:NO];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.model.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RPictureInfoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RPictureInfoCell ID] forIndexPath:indexPath];
    cell.img.image = [UIImage imageWithData:self.model.data[indexPath.row]];
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int i = scrollView.contentOffset.x / SCREEN_WIDTH + 1;
    self.title = [NSString stringWithFormat:@"%d/%d",i,(int)self.model.data.count];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"================ offsetx  %f",scrollView.contentOffset.x);
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(SCREEN_WIDTH,self.view.frame.size.height - self.tool.frame.size.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

@end
